<?php
class Block_Core_Abstract
    extends Object
{
    protected $_child = array();

    protected $_config;

    public function __construct()
    {
        $this->_config = Bootstrap::getStoreConfig();
    }

    public function setChild($block,$alias=null)
    {
        if(!$block) {
            return false;
        }

        if(is_string($block)) {
            $block = new $block();
        }

        if($alias) {
            $this->_child[$alias] = $block;
        } else {
            $this->_child[] = $block;
        }
        return true;
    }

    public function getChild($name)
    {
        if(isset($this->_child[$name])) {
            return $this->_child[$name];
        } else {
            return false;
        }
    }

    public function getChildHtml($name)
    {
        if(isset($this->_child[$name])) {
            return $this->_child[$name]->toHtml();
        } else {
            return false;
        }
    }

    public function isRecursiveChild($name)
    {
        if(isset($this->_child[$name])) {
            return $this->_child[$name];
        } else {
            foreach ($this->_child as $item) {
                $flag = $item->isRecursiveChild($name);
                if($flag) {
                    return $flag;
                }
            }
        }

        return false;
    }

    public function toHtml()
    {
        if($this->getTemplate()) {
            include $this->_config->path->design.'template/'.$this->getTemplate();
        } else {
            return '';
        }
    }

    public function getUrl($path=null)
    {
        return Bootstrap::getUrl($path);
    }

    public function getSkinUrl($path=null)
    {
        return $this->getUrl('design/admin/default/skin/'.$path);
    }

    public function deleteChilds()
    {
        $this->_child = array();
        return $this;
    }
}